﻿using System.Collections;

namespace QuickRunCore.Helper
{
    public class JsonHelper
    {

        public JsonHelper()
        {

        }

        public JsonHelper(string js)
        {
            JsonStr = js;
        }

        /// <summary>
        /// 变量JSON字符串
        /// </summary>
        public string JsonStr
        {
            get { return GetJsonStr(); }
            set { SetJsonStr(value); }
        }

        Hashtable ht = new Hashtable();

        /// <summary>
        /// 增加变量
        /// </summary>
        /// <param name="VarName">变量名</param>
        /// <param name="VarValue">变量值</param>
        public JsonHelper AddVar(string VarName, object VarValue)
        {

            if (ht.Contains(VarName))
            {
                ht[VarName] = VarValue;
            }
            else
            {
                ht.Add(VarName, VarValue);
            }
            return this;

        }

        /// <summary>
        /// 获取变量值
        /// </summary>
        /// <param name="VarName">变量名</param>
        /// <returns></returns>
        public string GetVar(string VarName)
        {
            if (JsonStr.ToLower() == "null")
            {
                return null;
            }
            if (JsonStr == "")
            {
                return "";
            }
            if (ht.Contains(VarName))
            {
                if (ht[VarName] != null)
                    return ht[VarName].ToString();
            }
            return null;

        }

        /// <summary>
        /// 清除所有变量
        /// </summary>
        public void Clear()
        {
            ht.Clear();
        }

        /// <summary>
        /// 移除变量
        /// </summary>
        /// <param name="VarName"></param>
        public void RemoveVar(string VarName)
        {
            ht.Remove(VarName);
        }

        /// <summary>
        /// 通过变量表获取JSON字符串
        /// </summary> 
        /// <returns></returns>
        private string GetJsonStr()
        {
            return GetJsonStrFromObject(ht);
        }

        /// <summary>
        /// 将JSON字符串转换成变量表
        /// </summary>
        /// <param name="js"></param>
        private void SetJsonStr(string js)
        {
            ht = GetObjectFromJsonStr<Hashtable>(js);
        }

        /// <summary>
        /// 获取对象JSON字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string GetJsonStrFromObject(object o)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(o);
        }

        /// <summary>
        /// 根据JSON字符串获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static T GetObjectFromJsonStr<T>(string s)
        {
            if (string.IsNullOrWhiteSpace(s) || s.ToLower() == "null")
            {
                object _o = null;
                return (T)_o;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(s);
        }

        public static string JsonToXML(string s)
        {
            System.Xml.Linq.XNode node = Newtonsoft.Json.JsonConvert.DeserializeXNode(s, "Root");
            return node.ToString();
        }
    }
}